<?php
/**
 * 商家入注控制器
 */
class MerchantsSettledController extends UserLoginBaseC
{
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new MerchantsSettledModel;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		if(isset($_POST['MerchantsSettledModel']))
		{
			$model->attributes=$_POST['MerchantsSettledModel'];
			if($model->save()){
				$this->redirect(array('view','id'=>$model->MerchantsSettledID));
			}else{
				//添加的时候把上传了的游离的图片放到redis,以备下次添加的时候直接拿来用。
				//此用户上传这证书的图片
				if(!empty($_POST['MerchantsSettledModel']['BusinessLicenseImg'])){
					RedisCluster::getInstance()->set('MerchantsSettled_BusinessLicenseImg' . yii::app()->user->id);
				}
				if(!empty($_POST['MerchantsSettledModel']['OrganizationImg'])){
					RedisCluster::getInstance()->set('MerchantsSettled_OrganizationImg' . yii::app()->user->id);
				}
				if(!empty($_POST['MerchantsSettledModel']['TaxRegistrationImg'])){
					RedisCluster::getInstance()->set('MerchantsSettled_TaxRegistrationImg' . yii::app()->user->id);
				}
				if(!empty($_POST['MerchantsSettledModel']['PositiveIDImg'])){
					RedisCluster::getInstance()->set('MerchantsSettled_PositiveIDImg' . yii::app()->user->id);
				}
				if(!empty($_POST['MerchantsSettledModel']['ReverseIDImg'])){
					RedisCluster::getInstance()->set('MerchantsSettled_ReverseIDImg' . yii::app()->user->id);
				}
			}
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['MerchantsSettledModel']))
		{
			$model->attributes=$_POST['MerchantsSettledModel'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->MerchantsSettledID));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('MerchantsSettledModel');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new MerchantsSettledModel('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['MerchantsSettledModel']))
			$model->attributes=$_GET['MerchantsSettledModel'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return MerchantsSettledModel the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=MerchantsSettledModel::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param MerchantsSettledModel $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='merchants-settled-model-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	/**
	 * 删除图片
	 * @return [type] [description]
	 */
	public function actionDeleteImg(){
		$config = ConfigModel::model()->getConfig();
		if(empty($_POST['filename'])){
			$this->error('参数有误');
		}		
        require_once Yii::app()->basePath . '/extensions/Oss/sdk.class.php';
        $oss_sdk_service = new ALIOSS();
        $oss_sdk_service->set_debug_mode(true);
        $oss_sdk_service->delete_object(UploadFile::getInstance()->bucket, $_POST['filename']);
	}

	/**
	 * 上传图片
	 * @return [type] [description]
	 */
	public function actionUpload(){
		//上传后返回其路径。
		if(UploadFile::getInstance()->upload(false, $_POST['path'], $_POST['name'])){
			$updateData = UploadFile::getInstance()->getUploadFileInfo();
			echo $updateData[0]['savepath'] . $updateData[0]['savename'];
			exit;
		}
	}
}
